rm(list= ls())

library(sf)
library(tidyverse)
library(lfe)

## Load de juan et. al data (cross-section for Nov 32)

dat <- readRDS('data/de_juan_et_al_32.rds')

## Load 1933 shaopefile 

shp_1933 <- read_sf('data/shape_33/German_Empire_1933_v.1.0.shp') %>% 
  st_transform(shp, crs = 4326) %>%
  st_make_valid()

## Load full list of synagogues
## Merge to 1933 counties 

syn <- read_csv('data/kristallnacht_syns_for_compiling.csv') %>%
  st_as_sf(., coords = c('lon', 'lat'), crs = 4326)  %>%
  st_transform(4326) %>%
  st_make_valid()

syn_by_county <- st_join(syn, shp_1933, join = st_within) %>%
  as.data.frame() %>%
  group_by(ID) %>%
  count() %>%
  ungroup() %>%
  rename(county_id = ID,
         n_syn_county = n)

merge_df <- tibble(county_id = unique(shp_1933$ID))  %>%
  left_join(syn_by_county) %>%
  mutate(n_syn_county = ifelse(is.na(n_syn_county), 0, n_syn_county),
         n_syn_county_bin = ifelse(n_syn_county > 0, 1, 0))

dat <- dat %>%
  left_join(merge_df, by = c('ID' = 'county_id'))

## Table 


m1 <- felm(nsdap_share ~ n_syn_county_bin | 0 | 0 | 0, data = dat)
m2 <- felm(nsdap_share ~ n_syn_county_bin | LAND | 0 | 0, data = dat)
m3 <- felm(spd_share ~ n_syn_county_bin | 0 | 0 | 0, data = dat)
m4 <- felm(spd_share ~ n_syn_county_bin | LAND | 0 | 0, data = dat)
m5 <- felm(kpd_share ~ n_syn_county_bin | 0 | 0 | 0, data = dat)
m6 <- felm(kpd_share ~ n_syn_county_bin | LAND | 0 | 0, data = dat)

mlist <- list(m1, m2, m3, m4, m5, m6)

## To Table 

mean(dat$nsdap_share, na.rm = T)
sd(dat$nsdap_share, na.rm = T)
mean(dat$spd_share, na.rm = T)
sd(dat$spd_share, na.rm = T)
mean(dat$kpd_share, na.rm = T)
sd(dat$kpd_share, na.rm = T)


stargazer::stargazer(mlist, 
                     style  = 'ajps',
                     keep = 'n_syn_county_bin',
                     covariate.labels = c('Synagogue in MP hometown'))




